/**   
 * @Title: ServerCommunicater.java 
 * @Package com.esun.network 
 * @Description: (Communicater父类 向服务器post信息) 
 * @author City Jiangsy@500wan.com   
 * @date 2011-5-31 下午08:30:37  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.network;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;

import com.esun.config.Config;
import com.esun.util.log.EDebug;

/**
 * @ClassName: ServerCommunicater
 * @Description: (Communicater 向服务器post信息,并取得返回数据)
 * @author City Jiangsy@500wan.com
 * @date 2011-5-31 下午08:30:37
 * 
 */
public class ServerCommunicater extends Errors {

	private String POST_START = "post start!!";
	private String POST_END = "post end!!\n============================";
	private String STR_DESCRIPTION = "服务器返回数据# \n \n";
	private String STR_NOMEAN = "\n \n";
	private String STR_HORIZONTAL = "============================";
	private String debugMsg;
	private ArrayList<Hashtable<String, String>> listResult;
	private Hashtable<String, String> result;
	private int state;

	private long CONNECTION_HEALTH_4 = 1000;
	private long CONNECTION_HEALTH_3 = 3000;
	private long CONNECTION_HEALTH_2 = 5000;
	private long CONNECTION_HEALTH_1 = 8000;
	private long CONNECTION_HEALTH_0 = 10000;
	private long requestTime = 0;

	private String serverMsg = "无信息";

	/**
	 * @Title: doPost
	 * @Description: (与服务器交互)
	 * @param @param postData 发送的信息
	 * @param @return serverStr
	 * @return String 返回类型
	 * @throws
	 */

	private String doPost(List<NameValuePair> params) throws ClientProtocolException, IOException {

		EDebug.o(POST_START);

		long beforeSend = System.currentTimeMillis();

		String strResult = null;
		HttpPost httpRequest = new HttpPost(Config.POST_ADDRESS);

		httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

		HttpParams httpParameters = new BasicHttpParams();

		HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);

		HttpConnectionParams.setSoTimeout(httpParameters, 10000);

		HttpResponse httpResponse = new DefaultHttpClient(httpParameters).execute(httpRequest);

		strResult = EntityUtils.toString(httpResponse.getEntity());

		EDebug.o(POST_END + STR_DESCRIPTION + strResult + STR_NOMEAN + Config.FUNSTRING + STR_HORIZONTAL);
		EDebug.o("string length is ==>>" + strResult.length());

		debugMsg = strResult;

		requestTime = System.currentTimeMillis() - beforeSend;
		EDebug.o(ServerCommunicater.this, "request time is " + requestTime);

		return strResult;
	}

	public void getServerResponse(ArrayList<HashMap<String, String>> dataPost, boolean isRSA) throws ClientProtocolException, IOException,
			ParserConfigurationException, SAXException {

		String strTemp = null;
		StringXML sx = new StringXML(dataPost);
		if (isRSA) {
			List<NameValuePair> listPost = sx.getListDoRSAAndBase64();
			listPost.add(new BasicNameValuePair(Config.PLATFORM, Config.ANDROID));
			strTemp = doPost(listPost);
		} else {
			List<NameValuePair> listPost = sx.getListNoRSAButBase64();
			listPost.add(new BasicNameValuePair(Config.PLATFORM, Config.ANDROID));
			strTemp = doPost(listPost);
		}

		XMLReaderAndPaser saxHandler = new XMLReaderAndPaser(strTemp);

		result = new Hashtable<String, String>();
		listResult = new ArrayList<Hashtable<String, String>>();

		result = saxHandler.getResult();
		listResult = saxHandler.getListResult();

		super.setErrorCode(result.get(Config.PASER_STATE));
		super.setServerMsg(result.get(Config.PASER_MSG));
		state = 0;
		state = Integer.parseInt((String) result.get(Config.PASER_STATE));

	}

	// ...
	public String getDebugMSG() {
		return debugMsg;
	}

	public ArrayList<Hashtable<String, String>> getListResult() {
		return listResult;
	}

	public Hashtable<String, String> getResult() {
		return result;
	}

	public int getState() {
		return state;
	}

	public String getServerMsg() {
		serverMsg = result.get(Config.PASER_MSG);
		return serverMsg;
	}

	public int getResponseHealthLevel() {

		if (requestTime <= 1000) {
			return 5;
		} else if (requestTime > CONNECTION_HEALTH_4 && requestTime <= CONNECTION_HEALTH_3) {
			return 4;
		} else if (requestTime > CONNECTION_HEALTH_3 && requestTime <= CONNECTION_HEALTH_2) {
			return 3;
		} else if (requestTime > CONNECTION_HEALTH_2 && requestTime <= CONNECTION_HEALTH_1) {
			return 2;
		} else if (requestTime > CONNECTION_HEALTH_1 && requestTime <= CONNECTION_HEALTH_0) {
			return 1;
		} else {
			return 0;
		}
	}
}
